<template>
  <div>
    <ImportExcel :on-success="success" />
  </div>
</template>

<script>
import { importEmployee } from '@/api/employee'
import { formatDate } from '@/utils'
export default {
  name: 'Import',
  data() {
    return {
      importData: [],
      keys: {
        '入职日期': 'timeOfEntry',
        '姓名': 'username',
        '工号': 'workNumber',
        '手机号': 'mobile',
        '转正日期': 'correctionTime',
        '聘用形式': 'formOfEmployment',
        '状态': 'enableState',
        '头像': 'staffPhoto'
      }
    }
  },
  methods: {
    success({ header, results }) {
      // console.log(this.$route.query)
      // 将results中的键的中文全部替换成英文
      if (this.$route.query.type === 'user') {
        results.map(obj => {
          const newObj = {}
          Object.keys(obj).forEach(item => {
            if (item === '入职日期' || item === '转正日期') {
              newObj[this.keys[item]] = formatDate(obj[item], '/)')
            } else {
              newObj[this.keys[item]] = obj[item]
            }
          })
          this.importData.push(newObj)
        })
        this.importEmployee()
        this.$message.success('批量导入用户成功')
        this.$router.back()
      }
    },
    // 批量导入员工
    async importEmployee() {
      await importEmployee(this.importData)
    }
  }
}
</script>

<style>

</style>
